Popular Logical Operators

The three most popular logical operators in Lua are and, or, and not.

Both and and or are what we call binary operators, since they compare two expressions and give us a final result true or false.

The and operator will only return true if both input expressions are also true.

The or operator will return true if at least one of the input expressions is true.

Also, it's important to know that both and and or use what we call “short-cut evaluation”. This means that they evaluate their second operand only when necessary. For example, if the first expression is false, the and operator will not bother checking the second expression, since its result will definitely be false. Lua uses short-cut evaluation to avoid pointless checks, and this is common not only in Lua but in many other programming languages.

The last logical operator I want to mention is the not operator. This is a unary operator (since it only needs one input) and it will simply negate the value of an expression.

In summary, the not operator returns true if the input expression is false, and returns false if the input expression is true.